// pages/address/user-address/user-address.js

var app = getApp()

Page ({
  data: {
    address   : [],
    radioindex: '',
    pro_id    : 0,
    num       : 0,
    cartId    : 0
  },

  onLoad: function (options) {
    var that = this;

    // 页面初始化 options为页面跳转所带来的参数
    var cartId = options.cartId;
    console.log(app.d.userId);

    if (cartId) {
      that.setData({
        cartId  : cartId,
      })
    }

    wx.request({
      url   : app.d.apiUrl + 'Address/index',
      data  : {
        user_id : app.d.userId,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {       // 设置请求的 header
        'Content-Type': 'application/x-www-form-urlencoded'
      },

      success: function(res) {
        // success
        var address = res.data.adds;
        console.log(address);

        if (address == '') {
          var address = []
        }

        that.setData({
          address : address
        })
      },

      fail: function() {
        // fail
        wx.showToast({
          title   : '网络异常！',
          duration: 2000
        });
      }
    })
  },

  setDefault: function(e) {
    var that = this;
    var addrId = e.currentTarget.dataset.id;

    wx.request({
      url   : app.d.apiUrl + 'Address/set_default',
      data  : {
        uid     : app.d.userId,
        addr_id : addrId
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {       // 设置请求的 header
        'Content-Type': 'application/x-www-form-urlencoded'
      },

      success: function(res) {
        // success
        var status = res.data.status;
        var cartId = that.data.cartId;

        if (status==1) {
          if (cartId) {
            wx.redirectTo({
              url: '/pages/order/pay?cartId=' + cartId,
            });

            return false;
          }

          wx.showToast({
            title   : '操作成功！',
            duration: 2000
          });

          that.DataonLoad();
        } else {
          wx.showToast({
            title   : res.data.err,
            duration: 2000
          });
        }
      },

      fail: function() {
        // fail
        wx.showToast({
          title   : '网络异常！',
          duration: 2000
        });
      }
    })
  },

  delAddress: function(e) {
    var that = this;

    var addrId = e.currentTarget.dataset.id;

    wx.showModal({
      title   : '提示',
      content : '您确认移除吗',

      success: function(res) {
        res.confirm && wx.request({
          url   : app.d.apiUrl + 'Address/del_adds',
          data  : {
            user_id : app.d.userId,
            id_arr  : addrId
          },
          method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          header: {       // 设置请求的 header
            'Content-Type': 'application/x-www-form-urlencoded'
          },

          success: function(res) {
            // success
            var status = res.data.status;

            if (status==1) {
              that.DataonLoad();
            } else {
              wx.showToast({
                title   : res.data.err,
                duration: 2000
              });
            }
          },

          fail: function() {
            // fail
            wx.showToast({
              title   : '网络异常！',
              duration: 2000
            });
          }
        });
      }
    });
  },

  DataonLoad: function() {
    var that = this;

    // 页面初始化 options为页面跳转所带来的参数
    wx.request({
      url   : app.d.apiUrl + 'Address/index',
      data  : {
        user_id : app.d.userId,
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {       // 设置请求的 header
        'Content-Type': 'application/x-www-form-urlencoded'
      },

      success: function(res) {
        // success
        var address = res.data.adds;
        if (address == '') {
          var address = []
        }
        that.setData({
          address: address,
        })
      },

      fail: function() {
        // fail
        wx.showToast({
          title   : '网络异常！',
          duration: 2000
        });
      }
    })
  },
})